﻿@using KPIS.GERP.Models.ICM
@using KPIS.GERP.Models.ICM.ViewModel
@model vmPrVsPo

@if (ViewContext.RouteData.Values["action"].ToString().ToLower() == "edit") { @Html.HiddenFor(model => model.PurchaseOrder.PoId) }
@Html.HiddenFor(model => model.PurchaseOrder.Code)
@Html.HiddenFor(model => model.PurchaseOrder.RecordStatus)
@Html.HiddenFor(model => model.PurchaseOrder.CreatedBy)
@Html.HiddenFor(model => model.PurchaseOrder.CreatedWhen)

@Html.HiddenFor(model => model.PurchaseOrder.UpdatedBy)
@Html.HiddenFor(model => model.PurchaseOrder.UpdatedWhen)
 
@{
    if (Model.Bidders != null)
    {

        <div class="row-fluid">
            <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
                <thead>
                    <tr>
                        <th class="center" style="width: 50px;">
                            <!-- Check box delete multiple records column-->
                            <label>
                                @*<input type="radio" />*@
                                <span class="lbl"></span>
                            </label>
                        </th> 
                        <th>
                            เลขขอซื้อ/จ้าง
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Bidders.FirstOrDefault().PurchaseRequest.Name)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Bidders.FirstOrDefault().PurchaseRequest.Reason)
                        </th>
                        <th>
                            วิธีการจัดหา
                        </th>
                        <th>
                            ปกติ/เร่งด่วน
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Bidders.FirstOrDefault().PurchaseRequest.PurchaseDate)
                        </th>
                        <th>
                            @* @if (Model.Bidders.FirstOrDefault().PurchaseRequest.RequireDuration.HasValue)
                        {
                            @Html.DisplayNameFor(model => model.Bidders.FirstOrDefault().PurchaseRequest.RequireDuration)
                        } else{
                                @Html.DisplayNameFor(model => model.Bidders.FirstOrDefault().PurchaseRequest.RequireDate)
                            }*@

                            @Html.DisplayName("วันที่ต้องการใช้งาน")
                        </th>
                        <th hidden>
                            @* @if (Model.Bidders.FirstOrDefault().PurchaseRequest.RequireDuration.HasValue)
                        {
                            @Html.DisplayNameFor(model => model.Bidders.FirstOrDefault().PurchaseRequest.RequireDuration)
                        } else{
                                @Html.DisplayNameFor(model => model.Bidders.FirstOrDefault().PurchaseRequest.RequireDate)
                            }*@

                            @*@Html.DisplayName("งานใบสั่ง/งานสัญญา")*@
                            @Html.DisplayName("ผู้ขาย")
                        </th>
                    </tr>
                </thead>

                <tbody>
                    @if (Model.Bidders.Any())
                    {
                        <tr>
                            <td colspan="100%" style="font-weight: bold" class="blue">
                                <div class="alert alert-block alert-success"> 
                                    มี <a href="@Url.Action("Index", "Purchase")">รายการขอซื้อ/ขอจ้าง</a> 
                                    ที่ได้รับอนุมัติใหม่ @Model.Bidders.Count รายการ
                                </div>
                                
                            </td>
                        </tr>
                        foreach (var m in Model.Bidders)
                        {

                            <tr> 
                                <td class="center">
                                    <label>
                                        <input type="radio" id="selectIds" name="selectIds" value="@(m.BidderId)" @((m.BidderId != null) ? "checked=\"checked\"" : "") />
                                             
                                        <span class="lbl"></span>
                                    </label>
                                </td>

                                <td>
                                    @Html.ActionLink(@m.PurchaseRequest.Code, "Details", "Purchase", new {Id = m.PrId}, null)  
                                    @Html.HiddenFor(model => m.PurchaseRequest.MasPurchaseMethod.IsBid)                         
                                    @Html.ValidationMessageFor(model => m.PrId)
                                </td>
                                <td>
                                    @Html.DisplayFor(model => m.PurchaseRequest.Name)                           
                                </td>
                                <td>
                                   @Html.Raw((m.PurchaseRequest.Reason ?? "").Replace("\n", "<br/>"))
                                </td>                               
                                <td>
                                @if (m.PurchaseRequest.MasPurchaseMethod == null)
                                        {
                                            var db = new GERPContext(false);
                                            m.PurchaseRequest.MasPurchaseMethod = db.MasPurchaseMethods.Find(m.PurchaseRequest.PcMethodId);
                                        }
                                    @Html.DisplayFor(model => m.PurchaseRequest.MasPurchaseMethod.Name)
                                </td> 
                                <td>
                                    @if (m.PurchaseRequest.Priority == null)
                                    {
                                        var db = new GERPContext(false);
                                        m.PurchaseRequest.Priority = db.IcmMasLookUps.Find(m.PurchaseRequest.PriorityId);
                                    }
                                    @if (m.PurchaseRequest.Priority.LookUpName == "ปกติ")
                                    {
                                        @Html.Raw("<font color='green'>")
                                    }
                                    else
                                    {
                                        @Html.Raw("<font color='red'>")
                                    }
                                    @Html.DisplayFor(model => m.PurchaseRequest.Priority.LookUpName)
                                    @Html.Raw("</font>")
                                </td>
                                <td>@Html.DisplayFor(model => m.PurchaseRequest.PurchaseDate)</td>
                                <td>
                                    @*@if (m.PurchaseRequest.RequireDuration.HasValue)
    {
        @Html.DisplayFor(model => m.PurchaseRequest.RequireDuration)
    }
    else
    {
        @Html.DisplayFor(model => m.PurchaseRequest.RequireDate)
    }
*@
                                    @{
                                        
                            var LookUpService = new KPIS.GERP.WEB.GIMS.IcmMasLookUpService.IcmMasLookUpServiceClient();
                            var durCountSelectList = LookUpService.MasLookUpSelectList(KPIS.GERP.Models.ICM.IcmMasLookUpType.LookUpType.DurationCount);
                            var durCountSelectListItem = durCountSelectList.FirstOrDefault(o => o.Value == (Model != null ? Convert.ToString(m.PurchaseRequest.DurCountId) : ""));
                            var durCountWording = durCountSelectListItem == null ? string.Empty : durCountSelectListItem.Text; 
                                    }
                                    

                                    @Html.DisplayFor(model => m.PurchaseRequest.RequireDate)
                                    @if (m.PurchaseRequest.DurTypeId == 10)
                                    {
                                        var task = new GERPContext().Tasks.OrderByDescending(o => o.TaskId).FirstOrDefault(o => o.RefId == m.PurchaseRequest.PrId && o.AppPrcId == 6);
                                        @:@Html.DisplayFor(model => m.PurchaseRequest.RequireDuration) วัน นับจาก @durCountWording




                                        if (m.PurchaseRequest.DurCountId == 10) //วันที่สร้างรายการ
                                        {
                                            if (m.PurchaseRequest.CreatedWhen.HasValue)
                                            {
                                                @: (วันที่สร้างรายการ คือ @m.PurchaseRequest.CreatedWhen.Value.ToString("dd MMM yyyy"))
                                            }
                                        }
                                        else if (m.PurchaseRequest.DurCountId == 11) // วันที่ยืนยันรายการ - ส่งขออนุมัติรายการ
                                        {
                                            if (task != null && task.CreatedWhen.HasValue)
                                            {
                                                @: (วันที่ยืนยันรายการ คือ @task.CreatedWhen.Value.ToString("dd MMM yyyy"))
                                            }
                                        }
                                        else if (m.PurchaseRequest.DurCountId == 12) // วันที่อนุมัติรายการ
                                        {
                                            if (task != null && task.ApprovedWhen.HasValue)
                                            {
                                                @: (วันที่อนุมัติรายการ คือ @task.ApprovedWhen.Value.ToString("dd MMM yyyy"))
                                            }
                                        }
                                        @:&nbsp;
                                    }
                                    else
                                    {
                                        @:วันที่ @(m.PurchaseRequest.RequireDate != null ? m.PurchaseRequest.RequireDate.Value.ToString("dd MMM yyyy") : "")
                                    }
                                    
                                    
                                    

                                </td>
                                <td hidden>
                                    @* @if(!m.MasPurchaseMethod.IsBid.Value)
                                    {
                                        @Html.Raw("N")
                                    }
                                    else
                                    {
                                        @Html.Raw("Y")
                                    }*@
                                    @Html.DisplayFor(model => m.MasContact.NameTh)
                                         
                                </td>                 
                            </tr>
                        }
                    }
                    else
                    {
                        <tr>
                            <td colspan="100%" style="font-weight: bold" class="red">
                                <div class="alert alert-block alert-error"> 
                                     
                                    ...ไม่มี <a href="@Url.Action("Index", "Purchase")">รายการขอซื้อ/ขอจ้าง</a> ที่ได้รับอนุมัติใหม่จึงไม่สามารถสร้างรายการใบสั่งหรือสัญญาได้...
                                </div>
                                
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>

        if (Model.Bidders.Any() || !Request.RawUrl.ToLower().Contains("create"))
        {
            <hr />
            <div class="widget-box">
                <div class="widget-header header-color-blue2">
                    <h4 class="lighter smaller">สร้างใบสั่งซื้อ/จ้าง</h4>
                </div>

                <div class="widget-body">
                    <div class="widget-body-inner">
                        <div class="widget-main">
                            <div class="form-horizontal">
                                @if (Model != null && Model.PurchaseOrder != null && !string.IsNullOrWhiteSpace(Model.PurchaseOrder.Code))
                                {
                                    <div class="control-group">
                                        <div class="control-label">
                                            @Html.DisplayNameFor(model => model.PurchaseOrder.Code)
                                        </div>
                                        <div class="controls blue" >
                                            @Html.DisplayFor(model => model.PurchaseOrder.Code) 
                                        </div>
                                    </div>
                                }
                                <div class="control-group">
                                    <div class="control-label">
                                        เป็นใบสั่งหรือสัญญา
                                    </div>
                     
                                    <div class="controls">
                                        @{
                                            bool checkedIsTrueIsContract = Model != null && Model.PurchaseOrder != null && Model.PurchaseOrder.IsContract == true;

                                        } 
                         
                                        @*@Html.RadioButtonFor(model => model.PurchaseOrder.IsContract, false)<span class="lbl">  ใบสั่ง</span>
                        @Html.RadioButtonFor(model => model.PurchaseOrder.IsContract, true)<span class="lbl">  สัญญา</span>*@
                                        <input id="PurchaseOrder_IsContract" name="PurchaseOrder.IsContract" value="False" type="radio"
                                            @(!checkedIsTrueIsContract ? "checked='checked'" : "")><span class="lbl">  ใบสั่ง</span>
                                        <input id="PurchaseOrder_IsContract" name="PurchaseOrder.IsContract" value="True" type="radio"
                                            @(checkedIsTrueIsContract ? "checked='checked'" : "")><span class="lbl">  สัญญา</span>
                        
                        
                                        @Html.Raw("&nbsp<font color=red>*</font>")
                                        @Html.ValidationMessageFor(model => model.PurchaseOrder.OrderDate)
                                    </div>            
                                </div>
                                <div class="control-group">
                                    <div class="control-label">
                                        @Html.DisplayNameFor(model => model.PurchaseOrder.OrderDate)
                                    </div>
                                    <div class="controls"> 
                                        @Html.EditorFor(model => model.PurchaseOrder.OrderDate)  
                                        @Html.Raw("&nbsp<font color=red>*</font>")
                                        @Html.ValidationMessageFor(model => model.PurchaseOrder.OrderDate)
                                    </div>            
                                </div>
                                <div class="control-group">
                                    <div class="control-label">
                                        @Html.DisplayNameFor(model => model.PurchaseOrder.Name)
                                    </div>
                                    <div class="controls">   
                                        @Html.EditorFor(model => model.PurchaseOrder.Name)
                                        @Html.Raw("&nbsp<font color=red>*</font>")
                                        @Html.ValidationMessageFor(model => model.PurchaseOrder.Name)
                                    </div>            
                                </div>
                                <div class="control-group">
                                    <div class="control-label">
                                        @Html.DisplayNameFor(model => model.PurchaseOrder.CanInstallment)
                                    </div>
                                    <div class="controls"> 
                                        @*@Html.RadioButtonFor(model => model.PurchaseOrder.CanInstallment, "N")<span class="lbl">@Html.Raw("&nbspปรับจากระยะเวลารวม")</span>
                        @Html.RadioButtonFor(model => model.PurchaseOrder.CanInstallment, "Y")<span class="lbl">@Html.Raw("&nbspปรับเป็นงวด")</span>*@ 
                                        @{
                                            bool bY = Model != null && Model.PurchaseOrder != null && Model.PurchaseOrder.CanInstallment == "Y";
                                        }
                                        <input id="PurchaseOrder_CanInstallment" name="PurchaseOrder.CanInstallment" type="radio" value="N" @(!bY ? "checked='checked'" : "")>
                                        <span class="lbl">&nbsp;ปรับจากระยะเวลารวม</span>
                                        <input id="PurchaseOrder_CanInstallment" name="PurchaseOrder.CanInstallment" type="radio" value="Y" @(bY ? "checked='checked'" : "")>
                                        <span class="lbl">&nbsp;ปรับเป็นงวด</span> 
                         

                                        @Html.Raw("&nbsp<font color=red>*</font>")
                                        @Html.ValidationMessageFor(model => model.PurchaseOrder.CanInstallment)
                                    </div>            
                                </div>
                                @*<div id="contract">
                <div class="control-group">
				    <div class="control-label">
						    @Html.DisplayNameFor(model => model.PurchaseOrder.RedemptionPercent)
				    </div>
                    <div class="controls">   
					    @Html.EditorFor(model => model.PurchaseOrder.RedemptionPercent)
                     
				    </div>            
			    </div>
                <div class="control-group">
				    <div class="control-label">
						    @Html.DisplayNameFor(model => model.PurchaseOrder.PrepayAmount)
				    </div>
                    <div class="controls">   
					    @Html.EditorFor(model => model.PurchaseOrder.PrepayAmount)
				    </div>            
			    </div>
                 <div class="control-group">
				    <div class="control-label">
						    @Html.DisplayNameFor(model => model.PurchaseOrder.CanSeparateDetail)
				    </div>
                    <div class="controls"> 
                        @Html.RadioButtonFor(model => model.PurchaseOrder.CanSeparateDetail, "Y")<span class="lbl">@Html.Raw("&nbspได้")</span>
                        @Html.RadioButtonFor(model => model.PurchaseOrder.CanSeparateDetail, "N")<span class="lbl">@Html.Raw("&nbspไม่ได้")</span> 
                      
				    </div>            
			    </div>
                </div>
                *@
                                <div class="control-group">
                                    <div class="control-label">
                                        @Html.DisplayName("หมายเหตุ")
                                    </div>
                                    <div class="controls">   
                                        @Html.TextAreaFor(model => model.PurchaseOrder.Descr)
                                        @*@Html.ValidationMessageFor(model => model.PurchaseOrder.Descr)*@
                                    </div>  
                                </div>
                                <div class="control-group">
                                    <div class="control-label">
                                        @Html.DisplayName("ระยะเวลาส่งมอบ")
                                    </div>
                                    <div class="controls"> 
                                        @{
                                            var LookUpService = new KPIS.GERP.WEB.GIMS.IcmMasLookUpService.IcmMasLookUpServiceClient();
                                            var durTypeSelectList = LookUpService.MasLookUpSelectList(KPIS.GERP.Models.ICM.IcmMasLookUpType.LookUpType.DurationType);
                                            var durType12 = durTypeSelectList.FirstOrDefault(o => o.Value == "12");
                                            var durType13 = durTypeSelectList.FirstOrDefault(o => o.Value == "13");
                                        }
                                        <div id="durType1">
                                            <label style="float: left; width: 120px">
                                                @Html.RadioButtonFor(m => m.PurchaseOrder.DurTypeId, 12, new {id = "durType1radio"})
                                                <span class="lbl"> @(durType12 != null ? durType12.Text : string.Empty) </span>&nbsp;&nbsp;&nbsp;
                                                @Html.ValidationMessageFor(model => model.PurchaseOrder.DurTypeId)
                                            </label>
                                            @Html.TextBoxFor(m => m.PurchaseOrder.RequireDuration, new {style = "width:60px"})
                                            @*Html.DropDownListFor(m=>m.PurchaseOrder.DurTypeId, durTypeSelectList, new {style="width:100px *@

                                            @Html.Raw("วัน&nbsp&nbspนับจากวันที่รับใบสั่ง&nbsp<font color=red>*</font>")
                                            @*@Html.DropDownListFor(m=>m.PurchaseOrder.DurCountId, durCountSelectList, new {style="width:200px"})*@
                                        </div>
                                        <div id="durType2" style="margin-top: 5px">
                                            <label style="float: left; width: 120px">
                                                @Html.RadioButtonFor(m => m.PurchaseOrder.DurTypeId, 13, new {id = "durType2radio"})
                                                <span class="lbl"> @(durType13 != null ? durType13.Text : string.Empty) </span>&nbsp;&nbsp;&nbsp;
                                            </label>
                                            @Html.EditorFor(m => m.PurchaseOrder.RequireDate)
                                        </div>  
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div> 
                </div>
            </div>
        }
    }
}